package primetest.impl.service;

import java.io.IOException;
import java.io.Serializable;
import java.sql.Date;
import java.util.List;

import javax.inject.Inject;

import primetest.dto.TreatmentRecordDto;
import primetest.impl.dao.TreatmentRecordDao;
import primetest.impl.dao.Transaction;
import primetest.impl.service.TreatmentRecordService;

public class TreatmentRecordService implements  Serializable {

	/**
	 *
	 */
	private static final long serialVersionUID = -8060430686329121620L;
	private Transaction transaction;
	private TreatmentRecordDao treatmentRecordDao;

	@Inject
	public TreatmentRecordService(Transaction transaction,TreatmentRecordDao officeDao){
		this.transaction = transaction;
		this.treatmentRecordDao = officeDao;
	}

	public List<TreatmentRecordDto> getTreatmentRecordList() throws IOException {
		try{
			transaction.begin();
			List<TreatmentRecordDto> officeList = treatmentRecordDao.getTreatmentRecordList(transaction);
			//transaction.commit();
			return officeList;
		}catch(IOException e){
			transaction.rollback();
			throw e;
		}
	}

	public TreatmentRecordDto getTreatmentRecord(int treatmentRecordSeq) throws IOException {
		try{
			transaction.begin();
			TreatmentRecordDto treatmentRecordDto = treatmentRecordDao.getTreatmentRecord(transaction, treatmentRecordSeq);
			//transaction.commit();
			return treatmentRecordDto;
		}catch(IOException e){
			transaction.rollback();
			throw e;
		}
	}


	public void addTreatmentRecord(TreatmentRecordDto officeDto) throws IOException {
		try{
			transaction.begin();
			treatmentRecordDao.addTreatmentRecord(transaction, officeDto);
			transaction.commit();
		}catch(IOException e){
			transaction.rollback();
			throw e;
		}
	}


	public void updateTreatmentRecord(TreatmentRecordDto officeDto) throws IOException {
		try{
			transaction.begin();
			treatmentRecordDao.updateTreatmentRecord(transaction, officeDto);
			transaction.commit();
		}catch(IOException e){
			transaction.rollback();
			throw e;
		}
	}


	public void deleteTreatmentRecord(int officeSeq) throws IOException {
		try{
			transaction.begin();
			treatmentRecordDao.deleteTreatmentRecord(transaction, officeSeq);
			//treatmentRecordDao.getTreatmentRecordList(transaction);
			//transaction.commit();
		}catch(IOException e){
			transaction.rollback();
			throw e;
		}
	}

	public List<TreatmentRecordDto> getTreatmentRecordList(int patientSeq,
			int doctorSeq, int departmentSeq, Date fromDate, Date endDate, int status) throws IOException {
		try{
			transaction.begin();
			List<TreatmentRecordDto> officeList = treatmentRecordDao.getTreatmentRecordList(transaction,  patientSeq,
					 doctorSeq,  departmentSeq,  fromDate,  endDate, status);
			transaction.commit();
			return officeList;
		}catch(IOException e){
			transaction.rollback();
			throw e;
		}
	}
}
